
const fs = require('fs')
const path = require('path')
const axios = require('axios');
const $ = require("jquery");
const d3 = require("d3");
const cheerio = require("cheerio")
const ejs = require('ejs');



ejs.renderFile(path.resolve(__dirname,"./1.ejs"),(err,data)=>{
    if(err) console.err(err)
    document.getElementById("root").innerHTML = data;

    const h1 = d3.select('#imageBox');
    const h2 = d3.select('#imageBox1');
    const h3 = d3.select('#imageBox3');

    async function ZhiHu() {
        let zhuText = await (()=>{
            return new Promise((resolve,reject)=>{
                axios.get('https://www.zhihu.com/question/333507081')
                    .then(function (response,reject) {
                        let html = $(response.data)
                        resolve({
                            comment:html.find('.List-headerText').children('span').text(),
                            browse:html.find('.NumberBoard-itemValue:eq(1)').attr('title'),
                            fllow:parseInt(html.find('.NumberBoard-itemValue:eq(0)').attr('title')),
                        })
                    })
                    .catch(function (error) {
                        reject(error)
                    })
            })
        })()
        await (()=>{
            return new Promise((resolve,reject)=>{
                if(zhuText.browse!=h1.text()){
                    d3.transition(null).select('#imageBox3').transition().duration(1000)
                        .tween('text',()=> {
                            var j = d3.interpolateString(h3.text(), zhuText.comment);
                            return (t) => {
                                h3.text(parseInt(j(t)).toString())
                            };
                        })
                    d3.transition(null).select('#imageBox1').transition().duration(1000)
                        .tween('text',()=> {
                            var j = d3.interpolateString(h2.text(), zhuText.fllow);
                            return (t) => {
                                h2.text(parseInt(j(t)).toString())
                            };
                        })
                    d3.transition(null).select('#imageBox').transition().duration(1000)
                        .tween('text',()=> {
                            var j = d3.interpolateString(h1.text(), zhuText.browse);
                            return (t) => {
                                h1.text(parseInt(j(t)).toString())
                            };
                        })
                        .on('end',()=>{
                            resolve('done')
                        })

                }else{
                    resolve('done')
                }
            })

        })()
        ZhiHu()
    }
    ZhiHu()
})

















